/**
 * @Author 韦建韩
 * @Date 2022/4/19
 */
package com.example.devops.web.controller.applicationAdmin;

import com.example.devops.Manage.UserManager.UserInfoManage;
import com.example.devops.dao.UserMapper;
import com.example.devops.entity.common.Result;
import com.example.devops.entity.pojo.MirrorInformation;
import com.example.devops.entity.pojo.User;
import com.example.devops.exception.ValidationException;
import com.example.devops.service.IMirrorCreateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("mirror_create")
public class MirrorCreateController {
    @Autowired
    IMirrorCreateService mirrorCreateService;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("create")
    public Result create(@RequestBody MirrorInformation mirrorInformation) {
        User user = UserInfoManage.get();
        User user1 = userMapper.selectById(user.getId());
        Result result = null;
        if (user1.getPrivilege() != null && user1.getPrivilege() != null) {
            if (user1.getPrivilege().contains("1")) {
                result = mirrorCreateService.create(mirrorInformation);
            } else {
                throw new ValidationException("该人员没有创建镜像的权限");
            }
        }else{
            throw new ValidationException("该人员没有创建镜像的权限");

        }
        return result;
    }

    @RequestMapping("create_process")
    public Result createProcess(String projectName) {
        return mirrorCreateService.createProcess(projectName);
    }

    @RequestMapping("getMirrorList")
    public Result getMirrorList(Long id) {
        if (id == 1) {
            return mirrorCreateService.getMirrorList();
        } else {
            return mirrorCreateService.getMirrorListById(id);
        }
    }

    @RequestMapping("deleteMirror")
    public Result deleteMirror(@RequestBody MirrorInformation mirrorInformation) {

        return mirrorCreateService.deleteMirror(mirrorInformation);
    }
}
